package com.yash.rms.domain;

import com.yash.rms.util.Constants;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import flexjson.transformer.DateTransformer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "project")
@RooDbManaged(automaticallyDelete = true)
@RooJson
public class Project {

    @OneToMany(mappedBy = "projectId", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<ResourceAllocation> resourceAllocations;

    @OneToMany(mappedBy = "projectId", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<Timehrs> timehrss;

    @OneToMany(mappedBy = "currentProjectId", fetch = FetchType.LAZY)
    private Set<Resource> resources;

    public Set<com.yash.rms.domain.Resource> getResources() {
        return resources;
    }

    public void setResources(Set<com.yash.rms.domain.Resource> resources) {
        this.resources = resources;
    }

    public Set<com.yash.rms.domain.ResourceAllocation> getResourceAllocations() {
        return resourceAllocations;
    }

    public void setResourceAllocations(Set<com.yash.rms.domain.ResourceAllocation> resourceAllocations) {
        this.resourceAllocations = resourceAllocations;
    }

    public Set<com.yash.rms.domain.Timehrs> getTimehrss() {
        return timehrss;
    }

    public void setTimehrss(Set<com.yash.rms.domain.Timehrs> timehrss) {
        this.timehrss = timehrss;
    }

    public String toJson() {
        return new JSONSerializer().exclude("*.class").transform(new DateTransformer(Constants.DATE_PATTERN), Date.class).serialize(this);
    }

    public static com.yash.rms.domain.Project fromJsonToProject(String json) {
        return new JSONDeserializer<Project>().use(null, Project.class).use(Date.class, new DateTransformer(Constants.DATE_PATTERN)).deserialize(json);
    }

    public static String toJsonArray(Collection<com.yash.rms.domain.Project> collection) {
        return new JSONSerializer().exclude("*.class").transform(new DateTransformer(Constants.DATE_PATTERN), Date.class).serialize(collection);
    }

    public static Collection<com.yash.rms.domain.Project> fromJsonArrayToProjects(String json) {
        return new JSONDeserializer<List<Project>>().use(null, ArrayList.class).use("values", Project.class).use(Date.class, new DateTransformer(Constants.DATE_PATTERN)).deserialize(json);
    }
}
